package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Assert;
import com.sc.nft.dao.TradingPoolAccountDao;
import com.sc.nft.entity.TradingPoolAccount;
import com.sc.nft.entity.TradingPoolAccountLog;
import com.sc.nft.enums.TradingPoolAccountCategoryEnum;
import com.sc.nft.enums.TradingPoolAccountTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.TradingPoolAccountService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author PC
 * @description 针对表【sc_trading_pool_account(交易池账户)】的数据库操作Service实现
 * @createDate 2024-10-26 13:52:01
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TradingPoolAccountServiceImpl implements TradingPoolAccountService {

    private final TradingPoolAccountDao tradingPoolAccountDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean frozenToGiftIncomeAccount(Long daoId, BigDecimal amount) {
        Assert.isTrue(amount.compareTo(BigDecimal.ZERO) > 0, () -> new GlobalRunTimeException("金额必须大于0"));
        List<TradingPoolAccount> tradingPoolAccountList = tradingPoolAccountDao.listByDaoId(daoId);
        Assert.isTrue(CollectionUtil.isNotEmpty(tradingPoolAccountList), () -> new GlobalRunTimeException("冻结账户金额不足"));
        Assert.isTrue(tradingPoolAccountList.size() == 1, () -> new GlobalRunTimeException("冻结账户异常"));
        TradingPoolAccount tradingPoolAccount = tradingPoolAccountList.get(0);
        Assert.isTrue(tradingPoolAccount.getFrozenAccount().compareTo(amount) >= 0, () -> new GlobalRunTimeException("冻结账户金额不足"));
        Assert.isTrue(tradingPoolAccountDao.frozenToGiftIncomeAccount(tradingPoolAccount.getId(), amount), () -> new GlobalRunTimeException("冻结账号转账到礼包账号失败"));
        //region 礼包收入账户变化
        TradingPoolAccountLog tradingPoolAccountLog = new TradingPoolAccountLog();
        tradingPoolAccountLog.setDaoId(daoId);
        tradingPoolAccountLog.setOperationTime(DateTime.now());
        tradingPoolAccountLog.setQuantity(amount);
        tradingPoolAccountLog.setBeforeChange(tradingPoolAccount.getGiftIncomeAccount());
        tradingPoolAccountLog.setAfterChange(tradingPoolAccount.getGiftIncomeAccount().add(amount));
        tradingPoolAccountLog.setCategory(TradingPoolAccountCategoryEnum.GIFT_INCOME_ACCOUNT);
        tradingPoolAccountLog.setIsAdd(Boolean.TRUE);
        tradingPoolAccountLog.setSource(TradingPoolAccountTypeEnum.FROZEN_ACCOUNT);
        tradingPoolAccountLog.setBeneficiary(TradingPoolAccountTypeEnum.GIFT_INCOME_ACCOUNT);
        tradingPoolAccountLog.insert();
        //endregion
        //region 冻结账户变化
        tradingPoolAccountLog = new TradingPoolAccountLog();
        tradingPoolAccountLog.setDaoId(daoId);
        tradingPoolAccountLog.setOperationTime(DateTime.now());
        tradingPoolAccountLog.setQuantity(amount);
        tradingPoolAccountLog.setBeforeChange(tradingPoolAccount.getFrozenAccount());
        tradingPoolAccountLog.setAfterChange(tradingPoolAccount.getFrozenAccount().subtract(amount));
        tradingPoolAccountLog.setCategory(TradingPoolAccountCategoryEnum.FROZEN_ACCOUNT);
        tradingPoolAccountLog.setIsAdd(Boolean.FALSE);
        tradingPoolAccountLog.setSource(TradingPoolAccountTypeEnum.FROZEN_ACCOUNT);
        tradingPoolAccountLog.setBeneficiary(TradingPoolAccountTypeEnum.GIFT_INCOME_ACCOUNT);
        tradingPoolAccountLog.insert();
        //endregion
        return Boolean.TRUE;
    }

}




